# !/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
Time      : 2019/7/10 16:23
@Author   : gao
@Site     : 
@File     : configutil.py
@Software : PyCharm
"""

import configparser


class ConfigUtil(object):
    def __init__(self, config_path):
        self.con = configparser.ConfigParser()
        self.config_path = config_path

    def get_all_sections(self):
        self.con.read(self.config_path)
        return self.con.sections()

    def get_section_options(self, section):
        self.con.read(self.config_path)
        return self.con.options(section)

    def get_section_items(self, section):
        self.con.read(self.config_path, encoding="utf-8-sig")
        return self.con.items(section)

    def getstring_value_with_section_options(self, section, option):
        self.con.read(self.config_path, encoding="utf-8-sig")
        return self.con.get(section, option)

    def getint_value_with_section_options(self, section, option):
        self.con.read(self.config_path)
        return self.con.getint(section, option)

    def getboolean_value_with_section_options(self, section, option):
        '''
        get the bool type value of the option in section
        :param section: section
        :param option: option
        :return: the bool type value of the option in section
        :rtype: bool
        '''
        self.con.read(self.config_path)
        return self.con.getboolean(section, option)

    def getfloat_value_with_section_options(self, section, option):
        '''
        get the float type value of the option in section
        :param section: section
        :param option: option
        :return: the float type value of the option in section
        :rtype: float
        '''
        self.con.read(self.config_path)
        return self.con.getfloat(section, option)

    def add_section(self, section):
        '''
        add the section which is not in the config
        :param section: section
        :return: True or False(False as the section is already in the config)
        :rtype: bool
        '''
        if section in self.get_all_sections():
            return False
        else:
            self.con.add_section(section)
            self.con.write(open(self.config_path, "w"))
            return True

    def set_value(self, section, option, value):
        '''
        add the value of the option in section
        :param section: section
        :param option: option
        :param value: new value
        :return: True or False(False as the section is not in the config)
        :rtype: bool
        '''
        if section in self.get_all_sections():
            self.con.set(section, option, value)
            self.con.write(open(self.config_path, "w+"))
            return True
        else:
            return False

    def has_option(self, section, option):
        if option in self.get_section_options(section):
            return True
        else:
            return False

    def has_section(self, section):
        if section in self.get_all_sections():
            return True
        else:
            return False

    def write(self, f):
        self.con.write(f)
